package cn.edu.klmy.spring.security.core.userdetails;

import cn.edu.klmy.models.user.AuthenticationUser;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
import java.util.List;

/**
 * 选课系统认证用户
 *
 * @author 顾伟
 *         创建日期：13-3-2
 */
public class CourseSystemUser implements UserDetails {

    private AuthenticationUser user;

    private List<Integer> roleIds;

    public AuthenticationUser getUser() {
        return user;
    }

    public CourseSystemUser(AuthenticationUser user, List<Integer> roleIds) {
        this.user = user;
        this.roleIds = roleIds;
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {

        String[] roleString = new String[roleIds.size()];
        for (int i = 0; i < roleIds.size(); i++) {
            roleString[i] = roleIds.get(i).toString();
        }
        return AuthorityUtils.createAuthorityList(roleString);
    }

    @Override
    public String getPassword() {
        return user.getUserPassword();
    }

    @Override
    public String getUsername() {
        return user.getUserName();
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }
}
